package com.atguigu.auth.utils;

import com.atguigu.model.system.SysMenu;

import java.util.ArrayList;
import java.util.List;

//使用递归方法构建菜单
public class MenuHelper {
    /**
     * 使用递归方法建菜单
     * @param sysMenusList
     * @return
     */
    public static List<SysMenu> buildTree(List<SysMenu> sysMenusList) {
        List<SysMenu> trees = new ArrayList<>();
        for (SysMenu sysMenu : sysMenusList) {
            if (sysMenu.getParentId().longValue() == 0) {
                trees.add(findChildren(sysMenu, sysMenusList));
            }
        }
        return trees;
    }
        /**
         * 递归查找子节点
         * @param treeNodes
         * @return
         */
        public static SysMenu findChildren(SysMenu sysMenu, List<SysMenu> treeNodes) {
            sysMenu.setChildren(new ArrayList<SysMenu>());

            for (SysMenu it : treeNodes) {
                if(sysMenu.getId().longValue() == it.getParentId().longValue()) {
                    if (sysMenu.getChildren() == null) {
                        sysMenu.setChildren(new ArrayList<>());
                    }
                    sysMenu.getChildren().add(findChildren(it,treeNodes));
                }
            }
            return sysMenu;
        }
}
